package dnuifood_cjy.dnuifood_16110100529.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import dnuifood_cjy.dnuifood_16110100529.Adapter.ShopAdapter;
import dnuifood_cjy.dnuifood_16110100529.R;

import dnuifood_cjy.dnuifood_16110100529.bean.ShopBean;
import dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import dnuifood_cjy.dnuifood_16110100529.model.ShopModel;

import java.util.ArrayList;
import java.util.List;




public class HomeFragment extends Fragment implements OnRetrofitListener<List<ShopBean>> {
    private RecyclerView recyclerView;
    private ShopAdapter shopAdapter;
    public HomeFragment() {
        // Required empty public constructor
    }



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.tab01, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
        initData();
        recyclerView.setAdapter(new ShopAdapter(getActivity(),getData()));
    }

    void initViews(){
        recyclerView=getActivity().findViewById(R.id.rv01);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
    void initData(){
        ShopModel shopModel = new ShopModel();
        shopModel.getAllShops(this);
    }

    @Override
    public void onSuccess(List<ShopBean> shopBeans, int flag) {
        shopAdapter = new ShopAdapter(getActivity(),shopBeans);
        recyclerView.setAdapter(shopAdapter);
    }


    @Override
    public void onFailure(String string) {
        Toast.makeText(getActivity(),"获取店铺信息失败",Toast.LENGTH_SHORT).show();
    }



    private List<ShopBean> getData() {
        List<ShopBean> list = new ArrayList() ;
        ShopBean shopBean = new ShopBean();
        shopBean.setShopname("满口川湘菜");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(5);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("东北一家人");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(2);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("姐妹麻辣烫");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(3);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("西湖印象");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(4);
        list.add(shopBean);

        shopBean = new ShopBean();
        shopBean.setShopname("王子普");
        shopBean.setAddress("东软三期食堂");
        shopBean.setPic(R.drawable.ic_launcher_background+"");
        shopBean.setLevel(2);
        list.add(shopBean);

        return list;
    }


}